package com.aier.bean.invoice;

import com.aier.bean.common.CommonCode;
import com.aier.bean.common.FieldDescription;
import com.aier.bean.foreign.ForeignBase;
import com.aier.bean.forwarder.FreightForwarder;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 恒达货代发票bean
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName("hengda_invoice")
@ContentRowHeight(25)
@ColumnWidth(20)
@HeadStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
public class HengdaInvoice {
    /** 货箱编号 **/
    @TableField("box_no")
    @ExcelProperty(value = "货箱编号")
    @FieldDescription("货箱编号")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String boxNo;

    /** fba码 **/
    @TableField("fba_code")
    @ExcelProperty(value = "fba码")
    @FieldDescription("fba码")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String fbaCode;

    /** 跟踪号 **/
    @TableField("tracking_number")
    @ExcelProperty(value = "跟踪号")
    @FieldDescription("跟踪号")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String trackingNumber;

    /** 型号sku **/
    @TableField("sku")
    @ExcelProperty(value = "型号sku")
    @FieldDescription("型号sku")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String sku;
    
    @TableField("unit")
    @ExcelProperty(value = "发货单位")
    @FieldDescription("发货单位")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String unit;

    /** 业务类型 **/
    @TableField("business_type")
    @ExcelProperty(value = "业务类型")
    @FieldDescription("业务类型")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String businessType;

    /** 报关方式 **/
    @TableField("declaration_type")
    @ExcelProperty(value = "报关方式")
    @FieldDescription("报关方式")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String declarationType;

    /** 运输渠道 **/
    @TableField("transport_channel")
    @ExcelProperty(value = "运输渠道")
    @FieldDescription("运输渠道")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String transportChannel;

    /** 品名 **/
    @TableField("brand_name")
    @ExcelProperty(value = "品名")
    @FieldDescription("品名")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String brandName;

    /** 国家 **/
    @TableField("country")
    @ExcelProperty(value = "国家")
    @FieldDescription("国家")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String country;

    /** 仓库代码 **/
    @TableField("warehouse_code")
    @ExcelProperty(value = "仓库代码")
    @FieldDescription("仓库代码")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String warehouseCode;

    /** 仓库地址 **/
    @TableField("warehouse_address")
    @ExcelProperty(value = "仓库地址")
    @FieldDescription("仓库地址")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String warehouseAddress;

    /** 预计货箱数 **/
    @TableField("expect_box_num")
    @ExcelProperty(value = "预计货箱数")
    @FieldDescription("预计货箱数")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal expectBoxNum;

    /** 预计重量(KG) **/
    @TableField("expect_weight")
    @ExcelProperty(value = "预计重量(KG)")
    @FieldDescription("预计重量(KG)")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal expectWeight;

    /** 预计体积(立方) **/
    @TableField("expect_bulk")
    @ExcelProperty(value = "预计体积(立方)")
    @FieldDescription("预计体积(立方)")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal expectBulk;

    /** 中文品名 **/
    @TableField("chine_brand_name")
    @ExcelProperty(value = "中文品名")
    @FieldDescription("中文品名")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String chineBrandName;

    /** 英文品名 **/
    @TableField("en_brand_name")
    @ExcelProperty(value = "英文品名")
    @FieldDescription("英文品名")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String enBrandName;

    /** 箱数 **/
    @TableField("box_num")
    @ExcelProperty(value = "箱数")
    @FieldDescription("箱数")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal boxNum;

    /** 每箱数量 **/
    @TableField("per_box_qty")
    @ExcelProperty(value = "每箱数量")
    @FieldDescription("每箱数量")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal perBoxQty;

    /** 总数量 **/
    @TableField("total_qty")
    @ExcelProperty(value = "总数量")
    @FieldDescription("总数量")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal totalQty;

    /** 单价 **/
    @TableField("declared_unit_price")
    @ExcelProperty(value = "单价")
    @FieldDescription("单价")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal declaredUnitPrice;

    /** 总价 **/
    @TableField("total_price")
    @ExcelProperty(value = "总价")
    @FieldDescription("总价")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal totalPrice;

    /** 品牌 **/
    @TableField("brand")
    @ExcelProperty(value = "品牌")
    @FieldDescription("品牌")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String brand;

    /** 材质 **/
    @TableField("material")
    @ExcelProperty(value = "材质")
    @FieldDescription("材质")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String material;

    /** 用途 **/
    @TableField("use_scope")
    @ExcelProperty(value = "用途")
    @FieldDescription("用途")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String useScope;

    /** 海关编码 **/
    @TableField("customs_code")
    @ExcelProperty(value = "海关编码")
    @FieldDescription("海关编码")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String customsCode;

    /** 外箱长度(cm) **/
    @TableField("box_length")
    @ExcelProperty(value = "外箱长度(cm)")
    @FieldDescription("外箱长度(cm)")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal boxLength;

    /** 外箱宽度(cm) **/
    @TableField("box_width")
    @ExcelProperty(value = "外箱宽度(cm)")
    @FieldDescription("外箱宽度(cm)")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal boxWidth;

    /** 外箱高度(cm) **/
    @TableField("box_height")
    @ExcelProperty(value = "外箱高度(cm)")
    @FieldDescription("外箱高度(cm)")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal boxHeight;

    /** 单箱重量(KG) **/
    @TableField("weight")
    @ExcelProperty(value = "单箱重量(KG)")
    @FieldDescription("单箱重量(KG)")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal weight;

    /** 总重量(KG) **/
    @TableField("total_weight")
    @ExcelProperty(value = "总重量(KG)")
    @FieldDescription("总重量(KG)")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public BigDecimal totalWeight;

     /** 总方数 **/
     @TableField("total_volumetric")
     @ExcelProperty(value = "总方数")
     @FieldDescription("总方数")
     @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
     public BigDecimal totalVolumetric;


    /** 产品性质 **/
    @TableField("characteristics")
    @ExcelProperty(value = "产品性质")
    @FieldDescription("产品性质")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    public String characteristics;

    /** 采购单价(RMB)(买保险需填写） **/
    @TableField("purchase_price")
    @ExcelProperty(value = "采购单价(RMB)(买保险需填写）")
    @FieldDescription("采购单价(RMB)(买保险需填写）")
    private String purchasePrice;

    /** 采购总价(RMB)(买保险需填写） **/
    @TableField("purchase_total_price")
    @ExcelProperty(value = "采购总价(RMB)(买保险需填写）")
    @FieldDescription("采购总价(RMB)(买保险需填写）")
    private BigDecimal purchaseTotalPrice;

    @TableField("self_taxed")
    @FieldDescription("是否自税")
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    @ColumnWidth(10)
    @ExcelIgnore
    public String selfTaxed;

    public HengdaInvoice(ForeignBase foreignBase, FreightForwarder freightForwarder){
        this.setWarehouseCode(foreignBase.getWarehouseCode());
        this.setFbaCode(foreignBase.getFbaCode());
        this.setBoxNo(generateBoxNo(foreignBase.getFbaCode(),foreignBase.getBoxNum()));
        this.setWarehouseAddress(freightForwarder.getDestination());
        this.setTrackingNumber(foreignBase.getTrackingNumber());
        this.setBoxNum(foreignBase.getBoxNum());
        this.setWeight(foreignBase.getWeight());
        this.setPerBoxQty(foreignBase.getPerBoxQty());
        this.setTotalQty(foreignBase.getPerBoxQty()
                .multiply(foreignBase.getBoxNum()));
        this.setSku(foreignBase.getSku());
        this.setBoxLength(foreignBase.getBoxLength());
        this.setBoxWidth(foreignBase.getBoxWidth());
        this.setBoxHeight(foreignBase.getBoxHeight());
        this.setCountry(foreignBase.getCountry());
        this.setSelfTaxed(foreignBase.getSelfTaxed());
    }

    private String generateBoxNo(String fbaCode, BigDecimal boxNum){
        String result = fbaCode+"000001-\n"+fbaCode+String.format("%06d", boxNum.intValue());
        return result;
    }

}
